package com.dlwx.parttimetalent.views;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.NumberPicker;
import android.widget.TextView;

import com.dlwx.parttimetalent.R;
import com.dlwx.parttimetalent.inter.DialogInter;
import com.flyco.dialog.widget.base.BaseDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/11/22.
 */

public class PricingDialog extends BaseDialog {
    @BindView(R.id.np_time)
    NumberPicker npTime;
    @BindView(R.id.np_price)
    NumberPicker npPrice;
    @BindView(R.id.tv_cancel)
    TextView tvCancel;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    @BindView(R.id.et_money)
    EditText etMoney;
    private Context context;
    private DialogInter inter;
    private String[] times;
    private String[] prices_hours;
    private String[] prices_days;
    private String cusPrice;


    public PricingDialog(Context context, DialogInter inter) {
        super(context);
        this.context = context;
        this.inter = inter;
    }

    @Override
    public View onCreateView() {
        widthScale(0.95f);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_pricing, null);
        ButterKnife.bind(this, view);
//        initView();
        return view;
    }

    private void initView() {
    }

    @Override
    public void setUiBeforShow() {
        times = new String[]{"1小时", "1天"};
        npTime.setDisplayedValues(times);
        npTime.setMinValue(0);
        npTime.setMaxValue(times.length - 1);
        npTime.setValue(0);
//
        prices_hours = new String[]{"20元", "30元", "40元", "50元", "60元", "70元", "80元"
                , "90元", "100元", "150元", "200元", "250元", "300元", "350元", "400元"
                , "450元", "500元"};
        prices_days = new String[]{"60元", "70元", "80元"
                , "90元", "100元", "150元", "200元", "250元", "300元", "350元", "400元"
                , "450元", "500元", "550元", "600元", "650元", "700元", "750元", "800元", "850元", "900元", "950元", "1000元"};
        npPrice.setMinValue(0);
        npPrice.setMaxValue(prices_hours.length - 1);
        npPrice.setDisplayedValues(prices_hours);
        npPrice.setValue(0);
        npTime.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                int maxValue = npPrice.getMaxValue();
                String[] prices;
                if (newVal == 1) {
                    prices = new String[prices_days.length];
                    for (int i = 0; i < prices_days.length; i++) {
                        prices[i] = prices_days[i];
                    }
                } else {
                    prices = new String[prices_hours.length];
                    for (int i = 0; i < prices_hours.length; i++) {
                        prices[i] = prices_hours[i];
                    }
                }
                int daysLength = prices.length - 1;
                if (daysLength > maxValue) {
                    npPrice.setDisplayedValues(prices);
                    npPrice.setMaxValue(prices.length - 1);
                } else {
                    npPrice.setMaxValue(prices.length - 1);
                    npPrice.setDisplayedValues(prices);
                }
            }
        });
        etMoney.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                cusPrice=s.toString();
            }
        });

        npPrice.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
        npTime.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
    }

    @OnClick({R.id.tv_cancel, R.id.tv_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_cancel:
                this.dismiss();
                break;
            case R.id.tv_confirm:
                String price;
                String time = times[npTime.getValue()];
                    if (time.contains("天")) {
                        time = "1";
                        price = prices_days[npPrice.getValue()];
                    } else {
                        time = "2";
                        price = prices_hours[npPrice.getValue()];
                    }
                    price = price.substring(0, price.length() - 1);
                if(!TextUtils.isEmpty(cusPrice)){
                    price=cusPrice;
                }
                inter.getWorkInfo(time, price, null);
                this.dismiss();
                break;
        }
    }
}
